package cn.zn.modules.appoin.controller;

import cn.zn.framework.common.utils.PageUtils;
import cn.zn.framework.common.utils.Query;
import cn.zn.framework.common.utils.R;
import cn.zn.modules.appoin.entity.AtrSpecialEntity;
import cn.zn.modules.appoin.entity.AtrSpecialWorkTime;
import cn.zn.modules.appoin.service.AtrSpecialService;
import cn.zn.modules.appoin.service.AtrSpecialWorkTimeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 规则：总例外
 * @author zn
 * @email zhunahao@qq.com
 * @date 2020-09-01 14:15:28
 */
@RestController
@RequestMapping("appoin/atrspecial")
public class AtrSpecialController {

    @Autowired
    AtrSpecialService atrSpecialService;
    @Autowired
    AtrSpecialWorkTimeService atrSpecialWorkTimeService;

    /**
     * 列表
     */
    @PostMapping("/list/{ruleId}")
    public R list(@PathVariable("ruleId") String ruleId, @RequestParam Map<String, Object> params) {
        return R.ok().put("page", new PageUtils(atrSpecialService.page(new Query<AtrSpecialEntity>().getPage(params), new QueryWrapper<AtrSpecialEntity>()
                .eq("RULE_ID_", ruleId)
                .orderByDesc("SPECIAL_DATE_"))));
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {

        AtrSpecialEntity atrSpecialEntity = atrSpecialService.getById(id);
        List<AtrSpecialWorkTime> specialWorkTimes = atrSpecialWorkTimeService.list(new QueryWrapper<AtrSpecialWorkTime>()
                .eq("SPECIAL_ID_", id)
                .orderByAsc("START_TIME_"));
        atrSpecialEntity.setSpecialWorkTimes(specialWorkTimes);
        return R.ok().put("special", atrSpecialEntity);
    }

    /**
     * 新增
     *
     * @param atrSpecialEntity
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody AtrSpecialEntity atrSpecialEntity) {
        atrSpecialService.saveSpecial(atrSpecialEntity);
        return R.ok();
    }

    /**
     * 更新
     *
     * @param atrSpecialEntity
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody AtrSpecialEntity atrSpecialEntity) {
        atrSpecialService.updateSpecial(atrSpecialEntity);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R detailDelete(@RequestBody String[] ids) {
        atrSpecialService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
